

import RankMgr, { cityType, rankMgrType } from "../../../home/RankMgr";
import { mgrs, sers } from "../../system/OverallSystem ";
import { GAME_MODE } from "../game/gameMode_Json";

import PageBase from "./pageBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class selectArea extends PageBase {

    @property({ type: cc.Node, displayName: "内容挂载节点" })
    content: cc.Node = null;
    @property({ type: cc.Prefab, displayName: "地区物品的预制体" })
    diquObjPrefab: cc.Prefab = null;

    RankMgrScript: RankMgr = null;

    data: rankMgrType = null;

    curSelectDiQu: cc.Node = null;
    curSelectDiQuData: cityType = null;

    pageData= null


    start(): void {
        super.start();
        this.RankMgrScript = this.node.getComponent(RankMgr);
        this.data = this.RankMgrScript.getRankInfo();
        this.showDiQu();
    }

    /** 根据当前数据更新选择地区的显示 */
    showDiQu() {
        this.data.cityRankInfo.sort((a, b) => {
            return Math.random() - 0.5;
        })
        for (let i = 0; i < this.data.cityRankInfo.length; i++) {
            let useData = this.data.cityRankInfo[i];

            let nodes = cc.instantiate(this.diquObjPrefab);
            nodes.parent = this.content;

            nodes.getChildByName("label").getComponent(cc.Label).string = "" + useData.cityName;

            nodes.on(cc.Node.EventType.TOUCH_END, ((e) => {
                this.creatorObjClickEvent(e, nodes, useData);
            }).bind(this));
        }
    }

    /** 创建物品被点击的事件 */
    creatorObjClickEvent(e: cc.Event.EventTouch, targetNode: cc.Node, data: cityType) {
        mgrs().soundMgr.playSound('click');
        let select = targetNode.getChildByName("select");
        let label = targetNode.getChildByName('label')
        if (this.curSelectDiQu != null) {
            let oldSelect = this.curSelectDiQu.getChildByName("select");
            oldSelect.active = false;
            this.curSelectDiQu.getChildByName('label').color = (new cc.Color).fromHEX("#312c2c");
        }
        this.curSelectDiQu = targetNode;
        this.curSelectDiQuData = data;
        select.active = true;
        label.color = (new cc.Color).fromHEX("#f0d29c");
    }


    /** 选择地区 */
    selectDiQu() {
        mgrs().soundMgr.playSound('click');
        if (this.curSelectDiQu != null) {
            // GameDataHelp.setCurDiQuData(this.curSelectDiQuData);
            let UserData = sers().userSrv.getUserData()
            UserData.city = this.curSelectDiQuData
            sers().userSrv.setUserData(UserData)
            // this.close();
            this.closeSelectDiQu()
        }
        else {
            // sers().uiSrv.showMessage("请选择地区");
            this.closeSelectDiQu()
        }
        // mgrs().gameMgr.enterMode(GAME_MODE.MAJIANG);

    }

    /** 关闭所选地区 */
    closeSelectDiQu() {
        mgrs().soundMgr.playSound('click');
        this.pageData.closeCall && this.pageData.closeCall();
        this.node.destroy();
    }

}
